package com.hjm.mall.api;

import com.hjm.mall.api.vo.GoodsListVO;
import com.hjm.mall.entity.Goods;
import com.hjm.mall.service.impl.GoodsServiceImpl;
import com.hjm.mall.util.Result;
import com.hjm.mall.util.ResultGenerator;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@ResponseBody
@RequestMapping("/api")
@Api(value = "image",tags = "哈基米商城——商品接口")
public class GoodsController {
    @Autowired
    GoodsServiceImpl goodsService;

    @GetMapping("/goods/{id}")
    @ApiOperation(value = "根据id查找商品", notes = "返回json")
    public Result<String> getGoodsById(
            @ApiParam("商品id")
            @PathVariable("id")
                    Integer id){
        if (id==null)
            return ResultGenerator.genErrorResult(404,"商品id为空");
        Goods goods = goodsService.getGoodsById(id);
        if (goods==null){
            return ResultGenerator.genErrorResult(404,"找不到商品");
        }
        return ResultGenerator.genSuccessResult(goods);
    }
    @GetMapping("/goods")
    @ApiOperation(value = "根据店铺id查找商品", notes = "返回json")
    public Result<String> getGoodsByStoreId(
            @ApiParam("商品id")
            @RequestParam("store")
                    Integer storeId){
        if (storeId==null)
            return ResultGenerator.genErrorResult(404,"店铺id为空");
        List<Goods> goods = goodsService.getGoodsByStoreId(storeId);
        if (goods.size()==0){
            return ResultGenerator.genErrorResult(404,"找不到商品");
        }
        return ResultGenerator.genSuccessResult(goods);
    }

    @GetMapping("/goods/list")
    @ApiOperation(value = "根据店铺id查找商品", notes = "返回json")
    public Result<String> getGoodsVOByStoreId(
            @ApiParam("商品id")
            @RequestParam("store")
                    Integer storeId){
        List<GoodsListVO> goodsListVOList = goodsService.getGoodsVOByStoreId(storeId);
        if (goodsListVOList.size()==0){
            return ResultGenerator.genErrorResult(404,"找不到商品");
        }
        return ResultGenerator.genSuccessResult(goodsListVOList);
    }
    @GetMapping("/goods/random")
    @ApiOperation(value = "获取六条随机商品数据", notes = "返回json")
    public Result<String> getGoodsVOToHome(
            @ApiParam("随机商品数量")
            @RequestParam("num")
                    Integer num){

        return ResultGenerator.genSuccessResult(goodsService.getRandomGoodsList(num));
    }
}
